<?php

/**
 * Productos form base class.
 *
 * @package    tercermilenio
 * @subpackage form
 * @author     Your name here
 */
class BaseProductosForm extends BaseFormPropel
{
  public function setup()
  {
    $this->setWidgets(array(
      'pro_articulo'              => new sfWidgetFormInputHidden(),
      'pro_talle'                 => new sfWidgetFormInputHidden(),
      'pro_precio'                => new sfWidgetFormInput(),
      'pro_cantidad'              => new sfWidgetFormInput(),
      'pro_disponible'            => new sfWidgetFormInput(),
      'pro_ganancia'              => new sfWidgetFormInput(),
      'pro_descripcion'           => new sfWidgetFormInput(),
      'pro_marca'                 => new sfWidgetFormPropelChoice(array('model' => 'Marcas', 'add_empty' => false)),
      'pro_rubro'                 => new sfWidgetFormPropelChoice(array('model' => 'Rubros', 'add_empty' => true)),
      'pro_proveedor'             => new sfWidgetFormPropelChoice(array('model' => 'Proveedores', 'add_empty' => false)),
      'detalle_devoluciones_list' => new sfWidgetFormPropelChoiceMany(array('model' => 'Devoluciones')),
      'detalle_ventas_list'       => new sfWidgetFormPropelChoiceMany(array('model' => 'Ventas')),
      'detalle_compras_list'      => new sfWidgetFormPropelChoiceMany(array('model' => 'Compras')),
    ));

    $this->setValidators(array(
      'pro_articulo'              => new sfValidatorPropelChoice(array('model' => 'Productos', 'column' => 'pro_articulo', 'required' => false)),
      'pro_talle'                 => new sfValidatorPropelChoice(array('model' => 'Productos', 'column' => 'pro_talle', 'required' => false)),
      'pro_precio'                => new sfValidatorNumber(),
      'pro_cantidad'              => new sfValidatorInteger(),
      'pro_disponible'            => new sfValidatorInteger(array('required' => false)),
      'pro_ganancia'              => new sfValidatorNumber(array('required' => false)),
      'pro_descripcion'           => new sfValidatorString(array('max_length' => 255)),
      'pro_marca'                 => new sfValidatorPropelChoice(array('model' => 'Marcas', 'column' => 'mar_id')),
      'pro_rubro'                 => new sfValidatorPropelChoice(array('model' => 'Rubros', 'column' => 'rub_id', 'required' => false)),
      'pro_proveedor'             => new sfValidatorPropelChoice(array('model' => 'Proveedores', 'column' => 'pvd_id')),
      'detalle_devoluciones_list' => new sfValidatorPropelChoiceMany(array('model' => 'Devoluciones', 'required' => false)),
      'detalle_ventas_list'       => new sfValidatorPropelChoiceMany(array('model' => 'Ventas', 'required' => false)),
      'detalle_compras_list'      => new sfValidatorPropelChoiceMany(array('model' => 'Compras', 'required' => false)),
    ));

    $this->widgetSchema->setNameFormat('productos[%s]');

    $this->errorSchema = new sfValidatorErrorSchema($this->validatorSchema);

    parent::setup();
  }

  public function getModelName()
  {
    return 'Productos';
  }


  public function updateDefaultsFromObject()
  {
    parent::updateDefaultsFromObject();

    if (isset($this->widgetSchema['detalle_devoluciones_list']))
    {
      $values = array();
      foreach ($this->object->getDetalleDevolucioness() as $obj)
      {
        $values[] = $obj->getDetDevDevolucion();
      }

      $this->setDefault('detalle_devoluciones_list', $values);
    }

    if (isset($this->widgetSchema['detalle_ventas_list']))
    {
      $values = array();
      foreach ($this->object->getDetalleVentass() as $obj)
      {
        $values[] = $obj->getDetVtaVta();
      }

      $this->setDefault('detalle_ventas_list', $values);
    }

    if (isset($this->widgetSchema['detalle_compras_list']))
    {
      $values = array();
      foreach ($this->object->getDetalleComprass() as $obj)
      {
        $values[] = $obj->getDetCprCompra();
      }

      $this->setDefault('detalle_compras_list', $values);
    }

  }

  protected function doSave($con = null)
  {
    parent::doSave($con);

    $this->saveDetalleDevolucionesList($con);
    $this->saveDetalleVentasList($con);
    $this->saveDetalleComprasList($con);
  }

  public function saveDetalleDevolucionesList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['detalle_devoluciones_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (is_null($con))
    {
      $con = $this->getConnection();
    }

    $c = new Criteria();
    $c->add(DetalleDevolucionesPeer::DET_VTA_ARTICULO, $this->object->getPrimaryKey());
    DetalleDevolucionesPeer::doDelete($c, $con);

    $values = $this->getValue('detalle_devoluciones_list');
    if (is_array($values))
    {
      foreach ($values as $value)
      {
        $obj = new DetalleDevoluciones();
        $obj->setDetVtaArticulo($this->object->getPrimaryKey());
        $obj->setDetDevDevolucion($value);
        $obj->save();
      }
    }
  }

  public function saveDetalleVentasList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['detalle_ventas_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (is_null($con))
    {
      $con = $this->getConnection();
    }

    $c = new Criteria();
    $c->add(DetalleVentasPeer::DET_VTA_ARTICULO, $this->object->getPrimaryKey());
    DetalleVentasPeer::doDelete($c, $con);

    $values = $this->getValue('detalle_ventas_list');
    if (is_array($values))
    {
      foreach ($values as $value)
      {
        $obj = new DetalleVentas();
        $obj->setDetVtaArticulo($this->object->getPrimaryKey());
        $obj->setDetVtaVta($value);
        $obj->save();
      }
    }
  }

  public function saveDetalleComprasList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['detalle_compras_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (is_null($con))
    {
      $con = $this->getConnection();
    }

    $c = new Criteria();
    $c->add(DetalleComprasPeer::DET_CPR_ARTICULO, $this->object->getPrimaryKey());
    DetalleComprasPeer::doDelete($c, $con);

    $values = $this->getValue('detalle_compras_list');
    if (is_array($values))
    {
      foreach ($values as $value)
      {
        $obj = new DetalleCompras();
        $obj->setDetCprArticulo($this->object->getPrimaryKey());
        $obj->setDetCprCompra($value);
        $obj->save();
      }
    }
  }

}
